<?php
declare (strict_types = 1);

namespace app\model\communal;

use app\facade\StringFacade;
use app\model\concern\AmisTrait;
use Carbon\Carbon;
use think\Model;
use think\db\Query;
use think\model\relation\BelongsTo;
use think\model\concern\SoftDelete;
use think\model\relation\HasOne;

class OrderCourseModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '50300_order_course';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    // 设置json类型字段
    protected $json = ['privilege'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    /**
     * 软删除
     */
    use SoftDelete;
    protected $deleteTime = 'delete_at';
    protected $defaultSoftDelete = 0;

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'number', 'number_md5', 'user_id', 'course_id', 'course_name', 'course_image', 'course_expired_days',
        'course_content', 'course_type', 'course_chapter_section', 'total_amount', 'payment_amount', 'deduct_amount',
        'deduct_coin', 'payment_type', 'trade_status', 'type', 'expired_at', 'state', 'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'user_id', 'number', 'type', 'state', 'createtime',
        'expired_at',
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = [
    ];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = [
    ];

    /**
     * @var string[]
     */
    public static $itemColumn = ['id', 'type', 'state'];

    /**
     * course_image 修改
     *
     * @param $value
     * @return string|string[]
     */
    public function setCourseImageAttr($value)
    {
        return StringFacade::replaceDomainShowUrl($value);
    }

    /**
     * course_image 获取
     *
     * @param $value
     * @param $data
     * @return mixed|string
     */
    public function getCourseImageAttr($value, $data)
    {
        if (!empty($value)) {
            $value = StringFacade::splicingDomainShowUrl($value);
        }

        return $value;
    }

    /**
     * number 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchNumberAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('number', 'like', '%' . $value . '%');
        }
    }

    /**
     * expired_at 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchExpiredAtAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $currentAt = Carbon::now()->timestamp;
            $query->where(function ($qq) use ($currentAt) {
                $qq->where('expired_at', 0)
                    ->whereOr('expired_at', '>', $currentAt);
            });

            dd($query->buildSql(true));
        }
    }

    /**
     * @return BelongsTo
     */
    public function belongUser()
    {
        return $this->belongsTo(UserModel::class, 'user_id', 'id')
            ->bind([
                'user_name' => 'name'
            ]);
    }
}
